﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Peak.ApplicationServices;
using Peak.Core;
using Peak.Web.Controllers.Models;
using SharpArch.Web.ModelBinder;
using System.Text.RegularExpressions;

namespace Peak.Web.Controllers.Binders
{
  public class TimeSpanBinder : SharpModelBinder
  {
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
      ValueProviderResult valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
      if (valueProviderResult == null) 
        return null;

      string[] value = (string[])valueProviderResult.RawValue;

      TimeSpan timeSpan;
      if(DurationParser.TryParse(value[0], out timeSpan))
        return timeSpan;

      return new TimeSpan();
    }
  }
}